home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrecord-1.8.1 / lib / astoi.c next >
Encoding:
C/C++ Source or Header  |  1996-05-09  |  2.4 KB  |  122 lines

  1. /* @(#)astoi.c    1.5 96/05/09 Copyright 1985 J. Schilling */
  2. /*
  3.  *    astoi() converts a string to int
  4.  *    astol() converts a string to long
  5.  *
  6.  *    Leading tabs and spaces are ignored.
  7.  *    Both return pointer to the first char that has not been used.
  8.  *    Caller must check if this means a bad conversion.
  9.  *
  10.  *    leading "+" is ignored
  11.  *    leading "0"  makes conversion octal (base 8)
  12.  *    leading "0x" makes conversion hex   (base 16)
  13.  *
  14.  *    Copyright (c) 1985 J. Schilling
  15.  */
  16. /* This program is free software; you can redistribute it and/or modify
  17.  * it under the terms of the GNU General Public License as published by
  18.  * the Free Software Foundation; either version 2, or (at your option)
  19.  * any later version.
  20.  *
  21.  * This program is distributed in the hope that it will be useful,
  22.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  * GNU General Public License for more details.
  25.  * 
  26.  * You should have received a copy of the GNU General Public License
  27.  * along with this program; see the file COPYING.  If not, write to
  28.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  29.  */
  30.  
  31. #include <standard.h>
  32.  
  33. #define is_space(c)     ((c) == ' ' || (c) == '\t')
  34. #define is_digit(c)     ((c) >= '0' && (c) <= '9')
  35. #define is_hex(c)    (((c) >= 'a' && (c) <= 'f') || \
  36.              ((c) >= 'A' && (c) <= 'F'))
  37.  
  38. #define to_lower(c)    (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A'+'a' : (c))
  39.  
  40. #ifdef    notdef
  41. int atoi(s)
  42.     char    *s;
  43. {
  44.     long    l;
  45.  
  46.     (void)astol(s, &l);
  47.     return (int) l;
  48. }
  49.  
  50. long atol(s)
  51.     char    *s;
  52. {
  53.     long    l;
  54.  
  55.     (void)astol(s, &l);
  56.     return l;
  57. }
  58. #endif
  59.  
  60. char *astoi(s, i)
  61.     const char *s;
  62.     int *i;
  63. {
  64.     long l;
  65.     char *ret;
  66.  
  67.     ret = astol(s, &l);
  68.     *i = l;
  69.     return(ret);
  70. }
  71.  
  72. char *astol(s, l)
  73.     register const char *s;
  74.     long *l;
  75. {
  76.     int neg = 0;
  77.     register long ret = 0L;
  78.     register int base = 10;
  79.     register int digit;
  80.     register char c;
  81.     
  82.     while (is_space(*s))
  83.         s++;
  84.  
  85.     if (*s == '+') {
  86.         s++;
  87.     } else if (*s == '-') {
  88.         s++;
  89.         neg++;
  90.     }
  91.  
  92.     if (*s == '0') {
  93.         base = 8;
  94.         s++;
  95.         if (*s == 'x' || *s == 'X') {
  96.             s++;
  97.             base = 16;
  98.         }
  99.     }
  100.     for (;(c = *s) != 0; s++) {
  101.  
  102.         if (is_digit(c)) {
  103.             digit = c - '0';
  104.         } else if (is_hex(c)) {
  105.             digit = to_lower(c) - 'a' + 10;
  106.         } else {
  107.             break;
  108.         }
  109.  
  110.         if (digit < base) {
  111.             ret *= base;
  112.             ret += digit;
  113.         } else {
  114.             break;
  115.         }
  116.     }
  117.     if (neg)
  118.         ret = -ret;
  119.     *l = ret;
  120.     return ((char *)s);
  121. }
  122.